package br.com.candiolli.apoio.server;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import br.com.candiolli.apoio.dto.ClienteDTO;

@Path("/ClienteWs")
public class ClienteWS extends RestWS {

	@GET
	@Path("/buscarClientes")
	@Produces(MediaType.APPLICATION_JSON)
	public List<ClienteDTO> buscarClientes() {
		try {
			ResultSet result = executeQuery("SELECT * FROM test.cliente");
			
			List<ClienteDTO> list = new ArrayList<ClienteDTO>();
			
			if(result != null){
				while(result.next()){
					ClienteDTO c = new ClienteDTO();
					c.setId(result.getInt("idcliente"));
					c.setNome(result.getString("nome"));
					c.setCpfCnpj(result.getString("cpfcnpj"));
					c.setEmail(result.getString("email"));
					Date date = result.getDate("dt_abertura");
					c.setDtAbertura(new SimpleDateFormat("dd/MM/yyyy").format(date));
					Date dateCadastro = result.getDate("dt_cadastro");
					c.setDtCadastro(new SimpleDateFormat("dd/MM/yyyy").format(dateCadastro));
					c.setEndereco(result.getString("endereco"));
					c.setUf(result.getString("uf"));
					c.setNivelCliente(result.getString("nivel_cliente"));
					c.setPais(result.getString("pais"));
					c.setQtdFuncionarios(result.getInt("qtd_funcionarios"));
					list.add(c);
				}
			}
			return list;
					
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return new ArrayList<ClienteDTO>();
	}
	
	@GET
	@Path("/buscarClientePorId")
	@Produces(MediaType.APPLICATION_JSON)
	public ClienteDTO buscarClientePorId(int id) {
		try {
			ResultSet result = executeQuery("SELECT * FROM test.cliente where idcliente = "+id);
			
			ClienteDTO c = null;
			if(result != null){
				while(result.next()){
					c = new ClienteDTO();
					c.setId(result.getInt("idcliente"));
					c.setNome(result.getString("nome"));
					c.setCpfCnpj(result.getString("cpfcnpj"));
					c.setEmail(result.getString("email"));
					Date date = result.getDate("dt_abertura");
					c.setDtAbertura(new SimpleDateFormat("dd/MM/yyyy").format(date));
					Date dateCadastro = result.getDate("dt_cadastro");
					c.setDtCadastro(new SimpleDateFormat("dd/MM/yyyy").format(dateCadastro));
					c.setEndereco(result.getString("endereco"));
					c.setUf(result.getString("uf"));
					c.setNivelCliente(result.getString("nivel_cliente"));
					c.setPais(result.getString("pais"));
					c.setQtdFuncionarios(result.getInt("qtd_funcionarios"));
				}
			}
			return c;
					
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
